# coding=utf-8
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from backstage.users import forms
from backstage.users.utils import view_funcs


class ConsumerList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        """
        获取用户列表
        """
        form = forms.GetCustomerList(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        data = form.cleaned_data
        users = view_funcs.get_consumer_list(
            data["phone"], data["email"])
        return Response({
            "status": 200,
            "msg": "success",
            "data": users,
        })


class ConsumerAddressList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request, uid):
        """
        获取用户收货地址列表
        """
        address_list = view_funcs.get_consumer_address_list(uid)
        return Response({
            "status": 200,
            "msg": "success",
            "data": address_list,
        })


class ConsumerPrintOrderList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request, user_id):
        """
        获取用户打印订单列表
        """
        form = forms.GetPrintOrderList(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        data = form.cleaned_data
        order_list = view_funcs.get_consumer_print_order_list(
            data["limit"], data.get("gt_time"), data.get("uid"),
            data.get("nickname_like"))
        return Response({
            "status": 200,
            "msg": "success",
            "data": order_list,
        })


class ConsumerTranscriptionList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        """
        获取用户抄写列表
        """
        form = forms.GetTranscriptionList(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        data = form.cleaned_data
        transcription_list = view_funcs.get_consumer_transcription_list(
            data["gt_time"], data["nickname_like"])
        return Response({
            "status": 200,
            "msg": "success",
            "data": transcription_list,
        })


class ConsumerAssistanceList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        """
        获取用户助抄列表
        """
        form = forms.GetAssistanceList(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        data = form.cleaned_data
        assistance_list = view_funcs.get_consumer_assistance_list(
            data["gt_time"], data["nickname_like"])

        return Response({
            "status": 200,
            "msg": "success",
            "data": assistance_list,
        })


class ConsumerFavoriteList(APIView):

    authentication_classes = (SessionAuthentication,)
    permission_classes = (IsAdminUser,)

    def get(self, request):
        """
        获取用户收藏列表
        """
        form = forms.GetFavoriteList(request.query_params)
        if not form.is_valid():
            return Response({
                "status": 400,
                "msg": "parameter error",
            })

        data = form.cleaned_data
        favorite_list = view_funcs.get_consumer_favorite_list(
            data["gt_time"], data["nickname_like"])
        return Response({
            "status": 200,
            "msg": "success",
            "data": favorite_list,
        })
